#ifndef mtrlAlpha_h__
#define mtrlAlpha_h__

#include "..\..\base\SampleBase.h"
#include "..\..\base\D3DUtil.h"
#include "..\..\base\Vertex.h"

class MtrlAlpha : public SampleBase
{
private:
	typedef Vertex_PNT Vertex;

	ID3DXMesh*       m_teapot;
	D3DMATERIAL9     m_teapotMtrl;

	IDirect3DVertexBuffer9*   m_wallQuad;
	IDirect3DTexture9*        m_wallTexture;
	D3DMATERIAL9              m_wallMtrl;

public:
	virtual bool Setup()
	{
		m_teapotMtrl = d3dutil::GREEN_MTRL;
		m_teapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity

		m_wallMtrl = d3dutil::WHITE_MTRL;

		// create the teapot
		D3DXCreateTeapot(m_device, &m_teapot, NULL);

		// create the wall quad
		m_device->CreateVertexBuffer(
			6 * sizeof(Vertex),
			D3DUSAGE_WRITEONLY,
			Vertex::FVF,
			D3DPOOL_MANAGED,
			&m_wallQuad,
			0);

		Vertex *v;
		m_wallQuad->Lock(0, 0, (void**)&v, 0);

		v[0] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
		v[1] = Vertex(-10.0f, 10.0f,  5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
		v[2] = Vertex(10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);

		v[3] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
		v[4] = Vertex(10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
		v[5] = Vertex(10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

		m_wallQuad->Unlock();

		D3DLIGHT9  dir;
		::ZeroMemory(&dir, sizeof(dir));
		dir.Type     = D3DLIGHT_DIRECTIONAL;
		dir.Diffuse  = d3dutil::WHITE;
		dir.Specular = d3dutil::WHITE * 0.2f;
		dir.Specular = d3dutil::WHITE * 0.6f;
		dir.Direction = D3DXVECTOR3(0.707f, -1.0f, 0.707f);

		m_device->SetLight(0, &dir);
		m_device->LightEnable(0, true);

		m_device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
		m_device->SetRenderState(D3DRS_SPECULARENABLE, true);

		// Create texture
		D3DXCreateTextureFromFile(m_device, "crate.jpg", &m_wallTexture);

		// Set Texture Filter States
		m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

		// Set alpha blending states
		// use alpha in material's diffuse component for alpha
		m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
		m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

		// Set blending factors so that alpha component determines transparency.
		m_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

		// Set camera
		D3DXVECTOR3 pos(0.0f, 0.0f, -3.0f);
		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
		D3DXMATRIX  V;
		D3DXMatrixLookAtLH(&V, &pos, &target, &up);

		m_device->SetTransform(D3DTS_VIEW, &V);

		// Set projection matrix
		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(
			&proj, 
			D3DX_PI * 0.5f,
			(float)m_windowWidth / (float)m_windowHeight,
			1.0f,
			1000.0f);
		m_device->SetTransform(D3DTS_PROJECTION, &proj);

		return true;
	}
	
	void Cleanup()
	{
		d3dutil::Release<IDirect3DVertexBuffer9*>(m_wallQuad);
		d3dutil::Release<IDirect3DTexture9*>(m_wallTexture);
		d3dutil::Release<ID3DXMesh*>(m_teapot);
	}

	bool Display(float timeDelta)
	{
		if (m_device)
		{
			// increase/decrease alpha via keyboard input
			if (::GetAsyncKeyState('A') & 0x8000f)
				m_teapotMtrl.Diffuse.a += 0.1f * timeDelta;
			if (::GetAsyncKeyState('S') & 0x8000f)
				m_teapotMtrl.Diffuse.a -= 0.1f * timeDelta;

			if (m_teapotMtrl.Diffuse.a > 1.0f)
				m_teapotMtrl.Diffuse.a = 1.0f;
			if (m_teapotMtrl.Diffuse.a < 0.0f)
				m_teapotMtrl.Diffuse.a = 0.0f;
			
			// Render
			m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
				0xff000000, 1.0f, 0);
			m_device->BeginScene();

			// Draw the wall
			D3DXMATRIX W;
			D3DXMatrixIdentity(&W);
			m_device->SetTransform(D3DTS_WORLD, &W);
			m_device->SetFVF(Vertex::FVF);
			m_device->SetStreamSource(0, m_wallQuad, 0, sizeof(Vertex));
			m_device->SetMaterial(&m_wallMtrl);
			m_device->SetTexture(0, m_wallTexture);
			m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

			// Draw the teapot
			m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
			D3DXMatrixScaling(&W, 1.5f, 1.5f, 1.5f);
			m_device->SetTransform(D3DTS_WORLD, &W);
			m_device->SetMaterial(&m_teapotMtrl);
			m_device->SetTexture(0, 0);
			m_teapot->DrawSubset(0);
			m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

			m_device->EndScene();
			m_device->Present(0, 0, 0, 0);
		}
		return true;
	}

};


#endif